/*
 * @Author: 260156 260156@gree.com.cn
 * @Date: 2022-08-11 11:36:12
 * @LastEditors: 260156 260156@gree.com.cn
 * @LastEditTime: 2022-09-29 16:02:41
 * @FilePath: \gree-zst-web\src\utils\message.ts
 * @Description:
 *
 * Copyright (c) 2022 by 260156 260156@gree.com.cn, All Rights Reserved.
 */
import { ElMessage, ElMessageBox } from "element-plus";

interface Config {
  type?: "success" | "warning" | "error";
  title?: string;
  content: string;
}

type ApiPromise = () => Promise<any>;

/**
 * @description：返回接口调用的函数
 * @config：弹窗配置
 * @description: 删除弹窗
 * @successMessage: 接口调用成功后的提示信息
 * @return {Promise}
 */
export async function doApiWithMessageBox(
  apiPromise: ApiPromise,
  config: Config,
  successMessage?: string
): Promise<any> {
  try {
    const { type, title, content } = config;
    await ElMessageBox.confirm(content, title || "警告", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: type || "warning",
    });
    await apiPromise();
    ElMessage({
      type: "success",
      message: successMessage || "删除成功！",
    });
    return Promise.resolve();
  } catch (err) {
    console.log("err");
    return Promise.reject(err);
  }
}
